import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:thinking/config/_.dart';
import 'package:thinking/entity/_.dart';
import 'package:thinking/service/_.dart';
import 'package:thinking/utils/_.dart';
import 'package:thinking/views/Dynamic/DynamicItem.dart';
import 'package:thinking/widget/_.dart';


class SearchDynamicValue extends StatefulWidget{
  final String value;
  SearchDynamicValue({required this.value});
  @override
  _SearchDynamicValue createState() => _SearchDynamicValue(value: value);

}


class _SearchDynamicValue extends State<SearchDynamicValue> with AutomaticKeepAliveClientMixin<SearchDynamicValue>{
  final String value;
  List<DynamicDataEntity> dynamicData = [];
  late  PageEntity _pageEntity;
  late EasyRefreshController _easyRefreshController;
  late Future<dynamic> _function;
  _SearchDynamicValue({required this.value});

  @override
  bool get wantKeepAlive => true;

  @override
  void initState() {
    super.initState();
    _pageEntity = PageEntity();
    _easyRefreshController = EasyRefreshController();
    _function = future();
  }
  future() async {
    await getValue();
  }
  Future<void> getValue() async {
    var res =  (await SearchService.getSearchDynamicData(params: {
      "search_Value":value,
      "search_Page":_pageEntity.page,
      "search_count":_pageEntity.count,
      "user_Id": SPreferences().getInt('user_Id')
    }))!;
    if (!_pageEntity.canLoad) {
      BotToast.showText(text: '没有更多动态了哦~');
    } else {
      BotToast.showText(text: '更新了${res.length}条动态~');
    }
    if (res.length < _pageEntity.count && _pageEntity.canLoad) {
      _pageEntity.setPage = _pageEntity.page + 1;
      _pageEntity.setCanLoad = false;
    }
    dynamicData = [...dynamicData,...res];
    setState(() {

    });



  }

  Widget easyRefresh(Widget child) {
    return EasyRefresh(
      child: child,
      header: BezierCircleHeader(backgroundColor: ColorConfig.ThemeColor),
      footer: BezierBounceFooter(backgroundColor: ColorConfig.ThemeColor),
      enableControlFinishRefresh: false,
      enableControlFinishLoad: true,
      controller: _easyRefreshController,
      bottomBouncing: true,
      onLoad: () async {
        if (_pageEntity.canLoad){
          _pageEntity.setPage = _pageEntity.page + 1;
        }
        await getValue();
        _easyRefreshController.finishLoad(success: true);
      },
    );
  }

  @override
  void dispose() {
    _easyRefreshController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return FutureBuilder(
        future: _function,
        builder: (BuildContext context, AsyncSnapshot snapshot){
          return snapshot.connectionState == ConnectionState.done?
          easyRefresh(Empty(child: ListView(
            shrinkWrap: true,
            physics: BouncingScrollPhysics(),
            children: dynamicData
                .asMap().keys.map((e) => Column(
              children: [
                e == 0?Container():Line(),
                DynamicItem(
                  dynamicData: dynamicData[e],
                  key: ValueKey(dynamicData[e].dynamicId),
                )
              ],
            )).toList(),
          ),isShow: dynamicData.length == 0,)):Loading(height: 300,);
        });
  }

}